﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using BBB.Valuta.Entity;

namespace BBB.Valuta
{
    [Serializable]
    public class Settings
    {
        //private string dbPath = string.Empty;
        //private string outPath = string.Empty;

        //private SerializableDictionary<string, string> valuta = new SerializableDictionary<string, string>();
        //private SerializableDictionary<string, string> printers = new SerializableDictionary<string, string>();
        private List<Entity.Valuta> _valuts = new List<Entity.Valuta>();
        private List<Printer>_printers = new List<Printer>();
        private List<Department> _departments = new List<Department>();

        [NonSerialized]
        private static Settings _instance;

        private Settings()
        {
        
        }

        public static void LoadSettings ()
        {
            XmlSerializer xml = new XmlSerializer(typeof (Settings));
            string settingsFile = Environment.CurrentDirectory + @"\Settings.xml";
            if (File.Exists(settingsFile))
            {
                using (TextReader reader = new StreamReader(settingsFile))
                {
                    _instance = (Settings) xml.Deserialize(reader);
                }
            }
            else
            {
                _instance = new Settings();
            }
        }

        public static Settings GetInstance ()
        {
            if (_instance == null)
                LoadSettings();
            return _instance;
        }
        
        public static void SaveSettings ()
        {
            XmlSerializer xml = new XmlSerializer(typeof (Settings) );
            string settingsFile = Environment.CurrentDirectory + @"\Settings.xml";
            using (TextWriter writer = new StreamWriter(settingsFile))
            {
                xml.Serialize(writer, _instance);
            }
        }

        public List<Entity.Valuta> Valuta
        {
            get { return _valuts; }
            set { _valuts = value; }
        }

        public List<Printer> Printers
        {
            get { return _printers; }
            set { _printers = value; }
        }

        public List<Department> Departments
        {
            get { return _departments; }
            set { _departments = value; }
        }
            
    }
}
